package com.example.springbott.realm;

import com.example.springbott.bean.MarketAdmin;
import com.example.springbott.bean.MarketAdminExample;
import com.example.springbott.bean.MarketUser;
import com.example.springbott.bean.MarketUserExample;
import com.example.springbott.config.MarketToken;
import com.example.springbott.mapper.MarketAdminMapper;
import com.example.springbott.mapper.MarketPermissionMapper;
import com.example.springbott.mapper.MarketUserMapper;
import com.example.springbott.util.Count;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 做的事情就是提供信息👉认证信息、授权信息
 * @author 1784549954@qq.com
 * @since 2022/11/21 15:11
 */
@Component
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Autowired
    MarketPermissionMapper marketPermissionMapper;

    @Autowired
    MarketUserMapper marketUserMapper;
    //先认证后授权
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //根据传入的对象获取用户信息
        String type = ((MarketToken) authenticationToken).getType();
        String  username = (String) authenticationToken.getPrincipal();

        if (Count.TYPE_ADMIN.equals(type)){
            //查询username对应的密码
            MarketAdminExample marketAdminExample = new MarketAdminExample();
            MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
            criteria.andUsernameEqualTo(username);
            List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);
            if (marketAdmins.size() == 1){
                MarketAdmin marketAdmin = marketAdmins.get(0);
                //构造认证信息时可以放入用户信息，Principals认证成功之后的用户信息
                //放入这个信息是为了取出这个信息，需要什么就放入什么
                return new SimpleAuthenticationInfo(marketAdmin,marketAdmin.getPassword(),getName());
            }
        }else if (Count.TYPE_WX.equals(type)){
            //查询market_user表
            MarketUserExample marketUserExample = new MarketUserExample();
            MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
            criteria.andUsernameEqualTo(username);
            List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
            if (marketUsers.size() == 1){
                MarketUser marketUser = marketUsers.get(0);
                return new SimpleAuthenticationInfo(marketUser,marketUser.getPassword(),getName());
            }
        }


            return null;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获得Principal信息
        MarketAdmin principal = (MarketAdmin) principalCollection.getPrimaryPrincipal();
        Integer[] roleIds = principal.getRoleIds();
        List<String> list ;

        //根据用户信息查询所有授权信息
        //mybatis查询
        if (Arrays.asList(roleIds).contains(1)){
            list = marketPermissionMapper.selectAllPermission();
        }else {
            list = marketPermissionMapper.selectPermission(roleIds);
        }

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(list);
        return simpleAuthorizationInfo;

    }
}
